package com.example.administrator.rfidtestdemo410;
        import android.os.Handler;
        import android.os.Message;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import com.cbtService.AndroidSDK.HardwareControler;

        import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    private int serialID;
    private Thread thread;
    private byte[] temp;
    private EditText editText,card;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String id = MainActivity.toHexString1((byte[])msg.obj);
                    textView.setText(id);
                    System.out.println(id.length());
                    card.setText(id.substring(10,14));
                    editText.setText(id.substring(14,18));
                    break;
            }
        }
    };
    public static String toHexString1(byte[] b){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; ++i){
            buffer.append(toHexString1(b[i]));
        }

        return buffer.toString();
    }
    public static String toHexString1(byte b){
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1){
            return "0" + s;
        }else{
            return s;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public void init(){
        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.info);
        editText=(EditText) findViewById(R.id.editText);
        card=(EditText)findViewById(R.id.editText2);
        thread = null;
        HardwareControler._init();
        temp = new byte[15];

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                serialID = HardwareControler.openSerialPort("/dev/s3c2410_serial4", 115200, 8, 1);

                thread = new Thread(){
                    @Override
                    public void run() {
                        while (true){

                            Message msg = new Message();
                            msg.what = 1;
                            HardwareControler.read(serialID, temp, 14);
                            msg.obj = temp;
                            System.out.println(msg);
                            handler.sendMessage(msg);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };

                thread.start();
            }
        });
    }

}

